import { locations } from "../../../gpu/locations";
const PointsGeometry =/*wgsl*/`
#include <Camera>
#include <Trans_def>

 
 struct Vertex {
    @builtin(vertex_index) VertexID:u32,
    #if ~{def.position}
    @location(${locations.position[1]}) position : vec3<f32>,
    #endif
    #if ~{def.color}
    @location(${locations.color[1]}) color : vec3<f32>,
    #endif
    #if ~{def.size}
    @location(${locations.size[1]}) size : f32,
    #endif
    #if ~{def.rotation}
    @location(${locations.rotation[1]}) rotation : f32,
    #endif 
    #if ~{def.opacity}
    @location(${locations.opacity[1]}) opacity : f32,
    #endif 
    #if ~{def.visible}
    @location(${locations.visible[1]}) visible : f32,
    #endif 
  };
  struct VertexOutput {
    @builtin(position) Position : vec4<f32>,
    @location(0) localPos:vec3<f32>,
    #if ~{def.map}
    @location(1) texcoord:vec2<f32>,
    #endif
    @location(2) color:vec4<f32>,
  }
  @vertex
  fn main(vertexInput:Vertex) -> VertexOutput {
        var output:VertexOutput;
        var modelMatrix:mat4x4<f32>;
        var u_NormalMatrix:mat4x4<f32>;
        // 从trans中分解处模型矩阵与法向量矩阵
        #if ~{def.trans}
        modelMatrix=trans.modelMat;
        u_NormalMatrix=trans.normalMat;
        #endif
        #if ~{def.transinstance}
        modelMatrix=trans[vertexInput.index].modelMat;
        u_NormalMatrix=trans[vertexInput.index].normalMat;
        #endif
        if(vertexInput.visible > 0.5) {
          output.color = vec4(vertexInput.color, vertexInput.opacity);
        } else {
          output.color = vec4(0.0, 0.0, 0.0, 0.0);
        }
        let points = array(
          vec2f(-1, -1),
          vec2f( 1, -1),
          vec2f(-1,  1),
          vec2f(-1,  1),
          vec2f( 1, -1),
          vec2f( 1,  1),
        );
        let pos = points[vertexInput.VertexID];
        let c = cos(vertexInput.rotation);
        let s = sin(vertexInput.rotation);
        let rot = mat2x2f(
          c, s,
         -s, c,
       );
        var position:vec4<f32> = vec4(vertexInput.position, 1.0);
        var worldPosition= modelMatrix*position;
        var clipPos =camera.vp*worldPosition;
        let pointPos = vec4f(pos * vertexInput.size / camera.viewport* clipPos.w , 0, 0);
        output.Position = clipPos + pointPos;
        #if ~{def.map}
        output.texcoord = rot*pos * 0.5 + 0.5;
        #endif
        output.localPos=worldPosition.xyz/worldPosition.w;
        return output;
      }
`
export { PointsGeometry }